package itsur.arquitecturas.implicitinvocation;

/**************************************************************************************
** Class name: CourseReader
** Author: A.J. Lattanze
** Date: 12/29/99
** Version 1.0
***************************************************************************************
** Purpose: Assignment 3 for SE 17-655, Architectures for Software Systems. 
** (reused from assignment 2)
** This class will read from the InputFile and instantiate the Course objects in the
** system.  It is assumed that the InputFile:
**
**	1) is in the local directory
**	2) contains the courses
**	3) each line of input is read and expected in the following format
**
**		17651 A MW 1030 1200 Smith Models of Software Systems
**		|	| |	|	|	|		|
**		|	| |	|	|	Instructor	Course Title
**		|	| |	|	Stop Time
**		|	| |	Start Time
**		|	| Days
**		|	Section
**		Course Number
**
** The Courses.txt file has been provided as an example file for you to use.
**
** Abstract:
**
**	private Course CourseStudentText( String LineOfText ) - The purpose of the this 
** 	method is to parse lines of text that are read from the text file containing	
**	course information in the above format.  Note that this is a private method and
**	it is used exclusively by GetCourseList
**	
**	public CourseList GetCourseList() - This method reads a line of text from the 
** 	course file.  The	line of text is then passed to the ParseCourseText method 
**	where it is parsed and a Course object is returned.  The Course object is then 
**	added to the list.  When a null is read from the Course file the method 
**	terminates and returns the list to the caller.  A list that points to null 
**	is an empty list.
**
** Modification Log
**************************************************************************************
**
**************************************************************************************/

public class CourseReader extends LineOfTextFileReader
{
	CourseList ListOfCourses = new CourseList();

	private Course ParseCourseText( String LineOfText )
	{
	   	boolean Done;	// Loop terminator
		String Token;	// String token parsed from LineOfText
		int TokenCount;	// Number of tokens parsed
		int FrontIndex;	// Front index of token to parse
		int BackIndex;	// Back index of token to parse

		// Instantiate a course object

		Course NewCourse = new Course();

		TokenCount = 0;
		FrontIndex = 0;
		BackIndex = 0;
		Done = false;

		while( !Done )
		{
		   BackIndex = LineOfText.indexOf( ' ', FrontIndex );
				
 		   if ( TokenCount < 6 ) 
		   {
		      Token = LineOfText.substring( FrontIndex, BackIndex );
	
		   } else {
							
			Token = LineOfText.substring( FrontIndex );

		   } // if 

		   switch ( TokenCount )
		   {
			case 0: // Course number 
				  NewCourse.CourseID = Token;
				  FrontIndex = BackIndex + 1;
				  TokenCount ++;
				  break;

			case 1: // Course Section ID
				  NewCourse.Section = Token;
				  FrontIndex = BackIndex + 1;
				  TokenCount ++;
				  break;

			case 2: // Days of the week the course will be held on
				  NewCourse.Days = Token;
				  FrontIndex = BackIndex + 1;
				  TokenCount ++;
				  break;

			case 3: // Course start time
				  NewCourse.StartTime = Token;
				  FrontIndex = BackIndex + 1;
				  TokenCount ++;
				  break;

			case 4: // Course stop time
				  NewCourse.StopTime = Token;
				  FrontIndex = BackIndex + 1;
				  TokenCount ++;
				  break;

			case 5: // Course instructor
				  NewCourse.Instructor = Token;
				  FrontIndex = BackIndex + 1;
				  TokenCount ++;
				  break;

			case 6: // Course title or name of the course
				  NewCourse.CourseName = Token;
				  Done = true;
				  break;

		   } // end switch
		} // end while

		return( NewCourse );

 	} // ParseCourseText

	public CourseList GetCourseList( String InputFile )
	{
		String CourseText;
		boolean Done;
		CourseList ListObject = new CourseList();
	
		if ( OpenFile ( InputFile ) )
		{
		   Done = false;

		   while ( !Done ) 
	 	   {
			try
			{
  			   CourseText = ReadLineOfText();
			   
			   if ( CourseText == null ) 
			   {
				Done = true;

			   } else {

				ListObject.AddCourse( ParseCourseText(CourseText) );

			   } // if 

			} // try

			catch( Exception Error )
			{
		 	   return( null );

			} // catch
		
		   } // while		

		} else {

			return( null );

		} // if

		CloseFile();

		return( ListObject );

	} // GetCourseList


	CourseReader()
	{
		ListOfCourses = null;
	
	} // Constructor #1


	CourseReader( String InputFile )
	{
		ListOfCourses = GetCourseList( InputFile );
	
	} // Constructor #2

} // CourseReader